package at.com.vn.chess.service;

import java.util.ArrayList;

import at.com.vn.chess.bean.PrivateChatRoomBean;
import at.com.vn.chess.entity.ChatUser;
import at.com.vn.chess.entity.ChessBoardInfo;
import at.com.vn.chess.util.Const;
import vn.com.bip.office.smart.util.XMLHelper;

public class LeaveShogiService extends ShogiService {

	public LeaveShogiService( String inputStrXml ) {
		super( inputStrXml );
		logger.info( "Bat dau" );
	}

	@Override
	public void doService() {
		String errMessage = "Some error";
		outputStrXml = XMLHelper.convertMessageToXML("ChatPrivate", "ERR_MSG",
				"MSG", errMessage);

		if (inputStrXml.trim().equals("")) {
			return;
		}
		
		ChatUser currentUser;

		ChessBoardInfo chessBoardInfo = ( ChessBoardInfo ) XMLHelper.convertXMLToListObject( inputStrXml, ChessBoardInfo.class ).get( 0 );
		
		outputStrXml = inputStrXml;
		
		currentUser = new ChatUser();
		currentUser.setChatMsg( chessBoardInfo.getChatMsg() );
		currentUser.setIsActive( chessBoardInfo.getIsAct() );
		currentUser.setIsDeleted( chessBoardInfo.getIsDeleted() );
		currentUser.setPrivateRoomID( chessBoardInfo.getTId() );
		currentUser.setRoomID( chessBoardInfo.getRId() );
		currentUser.setUserID( chessBoardInfo.getUId() );
		
		PrivateChatRoomBean currentChatRoom = getChatRoomByID(currentUser
				.getPrivateRoomID());
		
		remove(currentUser, currentChatRoom.lstChatUser);
		currentChatRoom.lstChatUser.add(currentUser);
		
		String listUserXml = getMXLResult(inputStrXml, currentUser,
				currentChatRoom.lstChatUser);
		
//		for ( int i = 0; i < chessBoardInfoList.size(); i++ ) {
//			if ( chessBoardInfoList.get( i ).contains( chessBoardInfo.getRoomId() )
//					&& chessBoardInfoList.get( i )
//							.contains( String.valueOf( chessBoardInfo.getTableId() ) ) ) {
//				chessBoardInfoList.remove( i );
//				chessBoardInfoList.add( inputStrXml );
//				break;
//			}
//		}
		
		shogiProcess();
		
		outputStrXml = new StringBuilder().append( outputStrXml ).append( Const.SEPERATE_MARK_FOR_LEAVE ).append( listUserXml ).toString();

	}
	
	public PrivateChatRoomBean getChatRoomByID(int privateRoomID) {
		for (PrivateChatRoomBean chatRoom : privateChatRoomBeanList) {
			if (chatRoom.privateRoomID == privateRoomID) {
				return chatRoom;
			}
		}
		return null;
	}
	
	private void remove(ChatUser user, ArrayList<ChatUser> lstUser) {
		for (ChatUser iuser : lstUser) {
			if (iuser.getUserID().equals(user.getUserID())) {
				lstUser.remove(iuser);
				break;
			}
		}
	}
	
	private String getMXLResult(String xmlInfo, ChatUser activeUser,
			ArrayList<ChatUser> lstUser) {

		String processName = XMLHelper.getProcessName(xmlInfo);

		// Reset other user's status
		for (ChatUser otherUser : lstUser) {
			if (!activeUser.getUserID().equals(otherUser.getUserID())) {
				// Do not show chat message for other user
				otherUser.setIsActive(0);
			} else {
				// when left
				if (processName.contains("_left")) {
					// not in chess table
					otherUser.setIsDeleted(1);
				}
			}

		}

		// Broadcast it to all listening clients
		return XMLHelper.convertListObjectToXML(lstUser, processName,
				ChatUser.class);
	}

}
